﻿using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using Think9.Models;
using Think9.Services.Basic;
using Think9.Services.Flow;

namespace Think9.Controllers.Basic
{
    [Area("SysFlow")]
    public class PrcsNextController : BaseController
    {
        private FlowPrcsNextService FlowPrcsNextService = new FlowPrcsNextService();

        [HttpGet]
        public ActionResult Next(string listid, string flid)
        {
            ViewBag.listid = listid;
            ViewBag.flid = flid;
            CurrentPrcsEntity mPrcs = FlowCom.GetCurrentFlowStept(flid, listid);

            if (mPrcs.FlowType == "1")
            {
                return View("~/Areas/SysFlow/PrcsNext/PrcsNext1.cshtml");
            }
            else
            {
                return View("~/Areas/SysFlow/PrcsNext/PrcsNext2.cshtml");
            }
        }

        //固定流程返回下一步骤
        [HttpPost]
        public ActionResult GetNextStepList(string listid, string flid)
        {
            listid = listid == null ? "0" : listid;
            var result = new { code = 0, msg = "", count = 999999, data = FlowPrcsNextService.GetNextPrcsList(listid, flid) };
            return Json(result);
        }

        //固定流程返回回退步骤
        [HttpPost]
        public ActionResult GetBackStepList(string listid, string flid)
        {
            listid = listid == null ? "0" : listid;
            var result = new { code = 0, msg = "", count = 999999, data = FlowPrcsNextService.GetBackPrcsList(listid, flid) };
            return Json(result);
        }

        //固定流程
        [HttpPost]
        public ActionResult FlowPrcsNext01(string listid, IEnumerable<valueTextEntity> list)
        {
            string selectUserV = BasicHelp.GetTextFromList(list, "classid", "selectUserId").Trim();
            string selectid = BasicHelp.GetTextFromList(list, "classid", "selectPrcsId").Trim();
            string msg = BasicHelp.GetTextFromList(list, "classid", "msg").Trim();
            string remarks = BasicHelp.GetTextFromList(list, "classid", "remarks").Trim();
            string ispublic = "1";

            string err = FlowCom.SetFlowPrcsNext01(CurrentUser, listid, selectid, selectUserV, remarks, ispublic);

            if (err == "")
            {
                return Json(SuccessTip("转交成功"));
            }
            else
            {
                return Json(ErrorTip(err));
            }
        }

        //固定流程
        [HttpPost]
        public ActionResult FlowPrcsBack01(string listid, IEnumerable<valueTextEntity> list)
        {
            string selectUserV = BasicHelp.GetTextFromList(list, "classid", "selectUserId").Trim();
            string selectid = BasicHelp.GetTextFromList(list, "classid", "selectPrcsId").Trim();
            string msg = BasicHelp.GetTextFromList(list, "classid", "msg").Trim();
            string remarks = BasicHelp.GetTextFromList(list, "classid", "remarks").Trim();
            string ispublic = "1";

            string err = FlowCom.SetFlowPrcsBack01(CurrentUser, listid, selectid, selectUserV, remarks, ispublic);

            if (err == "")
            {
                return Json(SuccessTip("转交成功"));
            }
            else
            {
                return Json(ErrorTip(err));
            }
        }

        //自由流程
        [HttpPost]
        public ActionResult FlowPrcsNext02(string listid, IEnumerable<valueTextEntity> list)
        {
            string selectUserV = BasicHelp.GetTextFromList(list, "classid", "selectUserV").Trim();
            string msg = BasicHelp.GetTextFromList(list, "classid", "msg01").Trim();
            string remarks = BasicHelp.GetTextFromList(list, "classid", "remarks01").Trim();
            string selectid = "";
            string ispublic = "1";

            if (string.IsNullOrEmpty(selectUserV))
            {
                return Json(ErrorTip("请选择办理人！"));
            }

            string err = FlowCom.SetFlowPrcsNext02(CurrentUser, listid, selectid, selectUserV, remarks, ispublic);

            if (err == "")
            {
                return Json(SuccessTip("转交成功"));
            }
            else
            {
                return Json(ErrorTip(err));
            }
        }

        //自由流程
        [HttpPost]
        public ActionResult FlowPrcsFinish(string listid, IEnumerable<valueTextEntity> list)
        {
            string selectUserV = BasicHelp.GetTextFromList(list, "classid", "selectUserV").Trim();
            string msg = BasicHelp.GetTextFromList(list, "classid", "msg01").Trim();
            string remarks = BasicHelp.GetTextFromList(list, "classid", "remarks01").Trim();
            string selectid = "0";
            string ispublic = "1";

            string err = FlowCom.SetFlowPrcsNext02(CurrentUser, listid, selectid, selectUserV, remarks, ispublic);

            if (err == "")
            {
                return Json(SuccessTip("转交成功"));
            }
            else
            {
                return Json(ErrorTip(err));
            }
        }
    }
}